import zipfile


class ReadZipFile:
    def __init__(self, filePath):
        # print('init readFile')
        self.filePath = filePath
        self.file = self.readZip()

    def readZip(self):
        zip_file = zipfile.ZipFile(self.filePath, mode="r")
        return zip_file

    # 这里使用isdir函数主要用来判断是否文件夹是否存在
    def isdir(self, name):
        # return any(x.startswith("%s/" % name.rstrip("/")) for x in self.file.namelist())
        for i in self.file.namelist():
            if name in i:
                return True
        return False
    # 根据包内路径读取文件（binary）
    def readSubFile(self, subFilePath):
        self.file = self.readZip()
        # print(subFilePath)
        return self.file.read(subFilePath)

    def closeSubFile(self, subFilePath):
        self.file.close()
        return

    # 判断包内path是否是文件或者文件夹
    def isFileOrFolder(self, full_path):
        return

    # 判断包内path是否存在
    # 返回0说明不存在文件，返回1说明存在该文件。
    def pathIsExists(self, filename):
        full_path = zipfile.Path(self.file, filename)  # 判断文件夹存不存在
        if full_path.exists():
            return 1
        else:
            return 0
